#include"../include/data_process.h"

int h_min = 0;
int h_max = 45;
int s_min = 46;
int s_max = 255;
int v_min = 57;
int v_max = 255;

vector<vector<Point> > contours;
RotatedRect end_rect;
Point2f rect[4];
Rect show_rect;

Mat color_infer(Mat src,Mat dst)
{
    cvtColor(src, dst, COLOR_BGR2HSV);//转为HSV
    inRange(dst, Scalar(h_min, s_min, v_min), Scalar(h_max, s_max, v_max), dst);

    Mat element2 = getStructuringElement(MORPH_RECT, Size(5, 5));  
    Mat element1 = getStructuringElement(MORPH_RECT, Size(7, 7));  

    //erode(dst, dst, element2);//腐蚀 
    dilate(dst, dst, element1);//膨胀
    imshow("color",dst);
    return  dst;
}   


/*
void process(Mat src,Mat dst,string name)
{   
    dst = color_infer(src,dst);
    findContours(dst,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);

    for(auto i : contours)
    {
        //cout<<"num="<<i.size()<<endl;
        if(i.size()>80)
        {
        end_rect = minAreaRect(i);
        //end_rect.points(rect);
        show_rect = boundingRect(i);
        
        /
        for(int j=0; j<4; j++)
        {
            line(src, rect[j], rect[(j+1)%4], Scalar(0, 0, 255), 2, 8);  //绘制最小外接矩形每条边
        }
        /
        //cout<<"中心点坐标： "<<end_rect.center<<endl;
        circle(src, Point(end_rect.center.x, end_rect.center.y), 5, Scalar(0, 255, 0), -1, 8);  //绘制最小外接矩形的中心点
        rectangle(src, show_rect.tl(),show_rect.br(),Scalar(0, 0, 255), 3);

        if(x_cha.size()==0)
        {
            x_cha.push_back(end_rect.center.x);
        }
        if(x_cha.size()==1)
        {
            x_cha.push_back(end_rect.center.x);
            /
            cout <<"x_cha"<< " ";
            for (auto i = x_cha.begin(); i != x_cha.end(); i++) {
                cout << *i <<' ';
            }
            /
            if(abs(x_cha[1]-x_cha[0])<1.5)
            {
                cout<<"高点了！"<<endl;
                //删除第一个元素
	            vector<double>::iterator k = x_cha.begin();
	            x_cha.erase(k);

                if(time_cha.size()==0){
                cout<<"周期开始"<<endl;
                time_start = clock();
                time_cha.push_back(time_start);
                continue;
                }

                if(time_cha.size()==1)
                {
                    cout<<"周期结束"<<endl;
                    time_end = clock();
                    time_cha.push_back(time_end);
                }

                if(time_cha.size()==2)
                {
                    my_time = double(time_end - time_start) / CLOCKS_PER_SEC;  
                    printf( "%f seconds/n", my_time );  
                    if(my_time>0.5)
                    {
                        cout<<"符合"<<endl;
                        nihe_time.push_back(my_time);
                    }
                    
                    time_cha.clear();
                }
                cout<<"the size of nihe_time:"<<nihe_time.size();
                if(nihe_time.size()==3)
                {
                    double sumValue = accumulate(begin(nihe_time), end(nihe_time), 0.0); 
                    double meanValue = sumValue / nihe_time.size();  
                    cout<<"周期容器："<<" ";
                    for (auto i = nihe_time.begin(); i != nihe_time.end(); i++) {
                        cout << *i <<' ';
                    }
                    cout<<"周期为："<<meanValue<<endl;
                    nihe_time.clear();
                    return;
                }


            }
            if(abs(x_cha[1]-x_cha[0])>1.5)
            {
                //删除第一个元素
	            vector<double>::iterator k = x_cha.begin();
	            x_cha.erase(k);
            }
        }

        imshow(name,src);
        waitKey(20);
    }
    }
}
*/

void process(Mat src,Mat dst,string name)
{   
    dst = color_infer(src,dst);
    findContours(dst,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
    cout<<contours.size()<<endl;
    for(auto i : contours)
    {
    if(i.size()>30)
    {	
    
    end_rect = minAreaRect(i);
    end_rect.points(rect);
    //cout<<"中心点坐标： "<<end_rect.center<<endl;
    circle(src, Point(end_rect.center.x, end_rect.center.y), 5, Scalar(0, 255, 0), -1, 8);  //绘制最小外接矩形的中心点
    for(int j=0; j<4; j++)
    {
        line(src, rect[j], rect[(j+1)%4], Scalar(0, 0, 255), 2, 8);  //绘制最小外接矩形每条边
    }
}
    imshow(name,src);
    waitKey(10);
    }
}
